import fetchData from "@/components/js/fetchData";
import layersMap from "@/components/js/layerMap";
import highLightColor from "@/components/js/highColor";
export async function highLightForLineSearch(row) {
    let name = row['线型码']
    window.viewer.scene.primitives._primitives.forEach(tileset => {
        if (tileset instanceof Cesium.Cesium3DTileset) {
            tileset.style = new Cesium.Cesium3DTileStyle({
                color: {
                    conditions: [
                        ["${name} === '" + name + "'", 'color("red", 1)'.replace('red',highLightColor)],
                        ['true', 'color("white")']
                    ]
                },
            });
        }
    })



    let _layer = layersMap.find(l => l.id === row.configLayerId)
    const res = await fetchData(_layer.url)

    const feature = res.features.find(fea => fea.properties['线型码'] === row['线型码'])
    let highLightLayer = window.viewer.dataSources.getByName('highLightLayer')[0]
    if (!highLightLayer) {
        highLightLayer = new Cesium.CustomDataSource('highLightLayer')
        window.viewer.dataSources.add(highLightLayer)
    }
    // 绘制
    highLightLayer.entities.removeAll()
    feature.geometry.coordinates.forEach(coordinate => {
        let customCoordinate = coordinate.flat().map((it, idx) => {
            if (idx % 3 == 2) {
                return 1100 // 指定线的高度
            } else {
                return it
            }
        })
        highLightLayer.entities.add({
            name: "polyline",
            polyline: {
                show: true, //是否显示，默认显示
                positions: Cesium.Cartesian3.fromDegreesArrayHeights([...customCoordinate]),
                width: 1, //线的宽度（像素），默认为1
                granularity: Cesium.Math.RADIANS_PER_DEGREE,
                material: Cesium.Color.BLUE.withAlpha(0.1), //线的颜色，默认为白色
                // depthFailMaterial: Cesium.Color.RED, //线被遮挡部分的颜色，默认为空（不显示被遮挡的部分），设置后可显示被遮挡的部分
                arcType: Cesium.ArcType.NONE, //线的地理类型，NONE表示纯直线，GEODESIC表示为测地线，RHUMB表示等角航线，默认为测地线
                // arcType: Cesium.ArcType.GEODESIC,
                // arcType: Cesium.ArcType.RHUMB,
                clampToGround: false, //是否贴地，默认为否
                shadows: Cesium.ShadowMode.DISABLED, //是否显示光照阴影，默认为否
                // distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000000.0), //显示的视角距离条件，在该范围内显示，范围不显示，默认为空
                classificationType: Cesium.ClassificationType.BOTH,
                zIndex: 0, //显示深度，越大表示图层在上
            }
        })
    })
    window.viewer.flyTo(highLightLayer, {
        offset: new Cesium.HeadingPitchRange(0.0, -3.1415926 / 2.0, 70.0),
    })
}

export async function highLightForPointSearch(row) {
    let name = row['物探点号']
    window.viewer.scene.primitives._primitives.forEach(tileset => {
        if (tileset instanceof Cesium.Cesium3DTileset) {
            tileset.style = new Cesium.Cesium3DTileStyle({
                color: {
                    conditions: [
                        ["${name} === '" + name + "'", 'color("red", 1)'.replace('red',highLightColor)],
                        ['true', 'color("white",1.0)']
                    ]
                },
            });
        }
    })
    let layer = layersMap.find(l => l.pipelineType === row['管线类型'])

    const res = await fetchData(layer.url)
    const feature = res.features.find(fea => fea.properties['物探点号'] === row['物探点号'])
    var highLightLayer = window.viewer.dataSources.getByName('highLightLayer')[0]
    if (!highLightLayer) {
        highLightLayer = new Cesium.CustomDataSource('highLightLayer')
        window.viewer.dataSources.add(highLightLayer)
    }
    // 绘制
    highLightLayer.entities.removeAll()
    highLightLayer.entities.add({
        name: "point",
        // position: Cesium.Cartesian3.fromDegreesArrayHeights(feature.geometry.coordinates),
        position: Cesium.Cartesian3.fromDegrees(feature.geometry.coordinates[0], feature.geometry.coordinates[1], feature.geometry.coordinates[2]),
        point: {
            color: new Cesium.Color.fromCssColorString("#3388ff"),
            pixelSize: 15,
            outlineColor: new Cesium.Color.fromCssColorString("#ffffff"),
            outlineWidth: 2,
            // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,     //贴地
            // scaleByDistance: new Cesium.NearFarScalar(1000, 1, 1000000, 0.1),
            disableDepthTestDistance: Number.POSITIVE_INFINITY //不被遮挡c
        },
    })
    window.viewer.flyTo(highLightLayer, {
        offset: new Cesium.HeadingPitchRange(0.0, -3.1415926 / 2.0, 70.0),
    })
}


export async function highLightForMaterialQuery(row) {
    let name = row['线型码']
    window.viewer.scene.primitives._primitives.forEach(tileset => {
        if (tileset instanceof Cesium.Cesium3DTileset) {
            tileset.style = new Cesium.Cesium3DTileStyle({
                color: {
                    conditions: [
                        ["${name} === '" + name + "'", 'color("red", 1)'.replace('red',highLightColor)],
                        ['true', 'color("white")']
                    ]
                },
            });
        }
    })
    let layer = layersMap.find(l => l.pipelineType == $('#caizhiSearch_layerSelect').val())
    const res = await fetchData(layer.url)
    const feature = res.features.find(fea => fea.properties['线型码'] === row['线型码'])
    var highLightLayer = window.viewer.dataSources.getByName('highLightLayer')[0]
    if (!highLightLayer) {
        highLightLayer = new Cesium.CustomDataSource('highLightLayer')
        window.viewer.dataSources.add(highLightLayer)
    }
    // 绘制
    highLightLayer.entities.removeAll()
    feature.geometry.coordinates.forEach(coordinate => {
        let customCoordinate = coordinate.flat().map((it, idx) => {
            if (idx % 3 == 2) {
                return 1100 // 指定线的高度
            } else {
                return it
            }
        })
        highLightLayer.entities.add({
            name: "polyline",
            polyline: {
                show: true, //是否显示，默认显示
                positions: Cesium.Cartesian3.fromDegreesArrayHeights([...customCoordinate]),
                width: 2, //线的宽度（像素），默认为1
                granularity: Cesium.Math.RADIANS_PER_DEGREE,
                material: Cesium.Color.BLUE.withAlpha(0.1), //线的颜色，默认为白色
                // depthFailMaterial: Cesium.Color.RED, //线被遮挡部分的颜色，默认为空（不显示被遮挡的部分），设置后可显示被遮挡的部分
                arcType: Cesium.ArcType.NONE, //线的地理类型，NONE表示纯直线，GEODESIC表示为测地线，RHUMB表示等角航线，默认为测地线
                // arcType: Cesium.ArcType.GEODESIC,
                // arcType: Cesium.ArcType.RHUMB,
                clampToGround: false, //是否贴地，默认为否
                shadows: Cesium.ShadowMode.DISABLED, //是否显示光照阴影，默认为否
                // distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000000.0), //显示的视角距离条件，在该范围内显示，范围不显示，默认为空
                classificationType: Cesium.ClassificationType.BOTH,
                zIndex: 0, //显示深度，越大表示图层在上
            }
        })
    })
    window.viewer.flyTo(highLightLayer, {
        offset: new Cesium.HeadingPitchRange(0.0, -3.1415926 / 2.0, 20.0),
    })
}


export async function highLightForPipeDiameterQuery(row) {
    let name = row['线型码']
    window.viewer.scene.primitives._primitives.forEach(tileset => {
        if (tileset instanceof Cesium.Cesium3DTileset) {
            tileset.style = new Cesium.Cesium3DTileStyle({
                color: {
                    conditions: [
                        ["${name} === '" + name + "'", 'color("red", 1)'.replace('red',highLightColor)],
                        ['true', 'color("white")']
                    ]
                },
            });
        }
    })
    let layer = layersMap.find(l => l.pipelineType == $('#guanjingSearch_layerSelect').val())
    const res = await fetchData(layer.url)
    const feature = res.features.find(fea => fea.properties['线型码'] === row['线型码'])
    var highLightLayer = window.viewer.dataSources.getByName('highLightLayer')[0]
    if (!highLightLayer) {
        highLightLayer = new Cesium.CustomDataSource('highLightLayer')
        window.viewer.dataSources.add(highLightLayer)
    }
    // 绘制
    highLightLayer.entities.removeAll()
    feature.geometry.coordinates.forEach(coordinate => {
        let customCoordinate = coordinate.flat().map((it, idx) => {
            if (idx % 3 == 2) {
                return 1200 // 指定线的高度
            } else {
                return it
            }
        })
        highLightLayer.entities.add({
            name: "polyline",
            polyline: {
                show: true, //是否显示，默认显示
                positions: Cesium.Cartesian3.fromDegreesArrayHeights([...customCoordinate]),
                width: 2, //线的宽度（像素），默认为1
                granularity: Cesium.Math.RADIANS_PER_DEGREE,
                material: Cesium.Color.BLUE.withAlpha(0.1), //线的颜色，默认为白色
                // depthFailMaterial: Cesium.Color.RED, //线被遮挡部分的颜色，默认为空（不显示被遮挡的部分），设置后可显示被遮挡的部分
                arcType: Cesium.ArcType.NONE, //线的地理类型，NONE表示纯直线，GEODESIC表示为测地线，RHUMB表示等角航线，默认为测地线
                // arcType: Cesium.ArcType.GEODESIC,
                // arcType: Cesium.ArcType.RHUMB,
                clampToGround: false, //是否贴地，默认为否
                shadows: Cesium.ShadowMode.DISABLED, //是否显示光照阴影，默认为否
                // distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000000.0), //显示的视角距离条件，在该范围内显示，范围不显示，默认为空
                classificationType: Cesium.ClassificationType.BOTH,
                zIndex: 0, //显示深度，越大表示图层在上
            }
        })
    })
    window.viewer.flyTo(highLightLayer, {
        offset: new Cesium.HeadingPitchRange(0.0, -3.1415926 / 2.0, 20.0),
    })
}
